package com.neu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.entity.DTO.ReviewReportDTO;
import com.neu.entity.Project;
import com.neu.entity.Task;
import com.neu.entity.User;
import com.neu.mapper.ReviewMapper;
import com.neu.mapper.ReviewMapper1;
import com.neu.service.*;
import com.neu.mapper.ReviewMapper1;
import com.neu.service.ProjectService;
import com.neu.service.ReviewService1;
import com.neu.base.PageBean;
import com.neu.entity.Review;
import com.neu.service.TaskService;
import com.neu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Review> implements ReviewService {
    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private UserService userService;

    @Override
    public PageBean<Review> pagelist(Integer pageNum, Integer pageSize, String reviewerId){
        Page<Review> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Review> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Review::getReviewerId, reviewerId);
        //Page 是 IPage 的具体实现类
        Page<Review> reviewIPage = reviewMapper.selectPage(page, queryWrapper);
        PageBean<Review> pageBean = new PageBean<>(reviewIPage);
        return pageBean;
    }

    public PageBean<ReviewReportDTO> search(Map map){
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        String reviewerId = (String) map.get("reviewerId");

        String projectName = null;
        String status = null;
        LocalDate startTime = null;
        LocalDate endTime = null;
        if (map.get("name") != null && !StringUtils.isEmpty(map.get("name").toString())){
            projectName = map.get("name").toString();
        }
        if (map.get("status") != null && !StringUtils.isEmpty(map.get("status").toString())){
            status = (String) map.get("status");
        }
        if (map.get("startDate") != null && !StringUtils.isEmpty(map.get("startDate").toString()) &&
        map.get("endDate") != null && !StringUtils.isEmpty(map.get("endDate").toString())){
            startTime = LocalDate.parse(map.get("startDate").toString());
            endTime = LocalDate.parse(map.get("endDate").toString());
        }

        LambdaQueryWrapper<Review> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Review::getReviewerId, reviewerId);
        List<Review> reviewList = reviewMapper.selectList(queryWrapper);
        List<ReviewReportDTO> dtoList = new ArrayList<>();

        for (Review review : reviewList) {
            // 根据review中的projectId, taskId, reviewerId找到对应的对象
            Project project = projectService.getById(review.getProjectId());
            Task task = taskService.getById(review.getTaskId());
            User user = userService.getById(review.getReviewerId());

            if ((projectName == null || projectName.equals(project.getProjectName())) && (status == null ||
                    status.equals(project.getProjectStatus())) && ((startTime == null && endTime == null) || (((endTime.isAfter(task.getDueDate().toLocalDate()) || endTime.isEqual(task.getDueDate().toLocalDate())))
            && ((startTime.isBefore(task.getDueDate().toLocalDate()) || startTime.isEqual(task.getDueDate().toLocalDate()))))))
             {
                 ReviewReportDTO dto = new ReviewReportDTO();
                 dto.setProjectId(project.getProjectId());
                 dto.setProjectName(project.getProjectName());
                 dto.setDueTime(task.getDueDate());
                 dto.setCreateTime(task.getCreateTime());
                 dto.setUserName(user.getUsername());
                 dto.setTaskStatus(task.getTaskStatus());
                 dto.setProjectStatus(project.getProjectStatus());
                 dto.setReviewId(review.getReviewId());

                 dtoList.add(dto);
            }
        }

        PageBean<ReviewReportDTO> dtoPageBean = new PageBean<>(pageNum, pageSize, dtoList.size(), dtoList);

        return dtoPageBean;
    }
}
